java - Spring mvc Controller returns jsp code instead of jsp page
问题描述
my Spring mvc controller returns Jsp code instead of jsp page
controller
@RequestMapping(value="/authenticate")
public ModelAndView dashboard(HttpServletResponse response,HttpServletRequest request) throws IOException
{
String username=request.getParameter("userName");
String pws=request.getParameter("pws");
System.out.println(username+pws);
return new ModelAndView("dashboard");
}
so, its returning whole jsp code of dasboard
.
but want to return a view jsp. why its returning code ?
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>Scheduling Portal</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext*.xml</param-value>
</context-param>
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
edit
if request is
@RequestMapping(value="/")
instead of
@RequestMapping(value="/authenticate")
then it returns jsp page.
i am adding servlet.xml to review it please get me a solution please look at servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- welcome page -->
<mvc:view-controller path="/" view-name="login"/>
<!-- add AspectJ autoproxy support for AOP -->
<aop:aspectj-autoproxy>
<aop:include name="CmsLoggingAspect"/>
</aop:aspectj-autoproxy>
<bean id="CmsLoggingAspect" class="com.rasvek.cms.aspect.CmsLoggingAspect"/>
<!-- Add support for component scanning -->
<context:component-scan base-package="com.rasvek.cms" >
</context:component-scan>
<!-- Add support for conversion, formatting and validation support -->
<mvc:annotation-driven/>
<!-- Define Spring MVC view resolver -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
<mvc:resources mapping="/resources/**" location="/resources/" />
<!-- Step 1: Define Database DataSource / connection pool -->
<bean id="myDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="driverClass" value="com.mysql.jdbc.Driver" />
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/campus_guru_01?useSSL=false" />
<property name="user" value="root" />
<property name="password" value="root" />
<!-- <property name="jdbcUrl" value="jdbc:mysql://aahbs06vwbadaq.ccmhxvps7vly.ap-south-1.rds.amazonaws.com:3306" />
<property name="user" value="CampusGuruDB" />
<property name="password" value="campusgurur" /> -->
<!-- these are connection pool properties for C3P0 -->
<property name="minPoolSize" value="5" />
<property name="maxPoolSize" value="20" />
<property name="maxIdleTime" value="30000" />
</bean>
<!-- Difine multipart Resolver for file upload -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- max upload size in bytes -->
<property name="maxUploadSize" value="20971520" /> <!-- 20MB -->
<!-- max size of file in memory (in bytes) -->
<property name="maxInMemorySize" value="1048576" /> <!-- 1MB -->
</bean>
<!-- Step 2: Setup Hibernate session factory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="myDataSource" />
<property name="packagesToScan" value="com.rasvek.cg.entity" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<tx:annotation-driven/>
<!-- Step 3: Setup Hibernate transaction manager -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!-- Step 4: Enable configuration of transactional behavior based on annotations -->
<tx:annotation-driven transaction-manager="transactionManager" />
<mvc:default-servlet-handler/>
</beans>
do i need to change anything in my servlet.xml ?
解决方案
也许您的 servlet 没有或没有正确配置。您的 servlet.xml 中需要类似的内容。这是为了将正确的 jsps 渲染到视图。
<!-- Resolves view names to protected .jsp resources within the /WEB-INF/views directory -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
顺便说一下。你为什么不使用 @RequestParam 和 @PathVariable 来接收你的参数。这要容易得多。
我建议像这样调整你的 servlet.xml:
<mvc:annotation-driven>
<mvc:message-converters>
<bean class="org.springframework.http.converter.StringHttpMessageConverter"/>
<bean class="org.springframework.http.converter.ResourceHttpMessageConverter"/>
<bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"/>
</mvc:message-converters>
</mvc:annotation-driven>
这样您就可以从您的 http 请求和响应中获得资源转换。
推荐阅读
- php - 在codeigniter中将数据从数组传递到另一个数组的问题
- c - C - 从 string2 替换 string1 中最常见的字母到最不常见的字母
- c# - 如何在类中动态创建属性
- google-sheets - 带有公式的条件格式不会跨多行应用样式
- python - 如何清除谷歌工作表的所有内容?
- javascript - 为什么不将 Flask 变量传递给 html?
- python-3.x - GCP-App Engine API 不可用,py 3
- php - php DOMDocument 允许输入 HTML 中的空属性?
- c# - 如何在 UserControl 中知道 MainWindow (Applicatoin) 何时关闭?- WPF
- masonite - 是否有获取 masonite 存储目录的绝对路径的辅助方法?