Spring控制器无法将参数传递给jsp

时间:2018-07-17 10:28:10

标签: java spring-mvc jsp spring-boot jstl

我是春季新人。我正在尝试将参数从控制器发送到jsp文件。我已经按照春季教程学习了,但是我发送给jsp的参数值仍然为null。我不知道我的代码有什么问题。

这是我的代码, web.xml

type Person struct {
   name string
   age  int
   rank int
}

app-servlet.xml

<display-name>OPTIMA Dashboard</display-name>

<servlet>
    <servlet-name>app</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
    <multipart-config>
        <max-request-size>418018841</max-request-size>
        <file-size-threshold>1048576</file-size-threshold>
    </multipart-config>
</servlet>

<servlet-mapping>
    <servlet-name>app</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>

LoginController.java

<context:property-placeholder location="classpath:${mule.env}.properties" />
<mvc:annotation-driven />

<mvc:resources mapping="/production/**" location="/production/" />
<mvc:resources mapping="/admin/build/**" location="/build/" />
<mvc:resources mapping="/admin/css/**" location="/css/" />
<mvc:resources mapping="/admin/vendors/**" location="/vendors/" />
<mvc:resources mapping="/build/**" location="/build/" />
<mvc:resources mapping="/css/**" location="/css/" />
<mvc:resources mapping="/vendors/**" location="/vendors/" />
<mvc:resources mapping="/favicon.ico" location="/favicon.ico" />

<context:component-scan base-package="com.jpa.optima.admin.controller" />

<bean id="multipartResolver"
    class="org.springframework.web.multipart.support.StandardServletMultipartResolver" />

<bean id="viewResolver"
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/production/" />
    <property name="suffix" value=".jsp" />
</bean>

login.jsp

@Controller
@RequestMapping(value = "/admin")
public class LoginController {

@Autowired
private SessionProcessor sessionProcessor;
@Autowired
private HazelcastInstance instance;
@Autowired
private ContextLoader contextLoader;

@RequestMapping(value = "/login", method = RequestMethod.GET)
public ModelAndView login(Model model) {
    ModelAndView modelAndView = new ModelAndView("login");
    modelAndView.addObject("key", contextLoader.getSiteKey());
    modelAndView.addObject("login", new Login());
    return modelAndView;
}

任何人都可以帮助我找出我的代码有什么问题。

谢谢

2 个答案:

答案 0 :(得分:0)

在LoginController.java中,您应该使用POST方法,因为您是从jsp作为POST发送数据的。

@RequestMapping(value = "/login", method = RequestMethod.POST)

答案 1 :(得分:0)

我已经找到问题了。我只需要从以下位置更改代码即可:

@RequestMapping(value =“ / login”,方法= RequestMethod.GET)

成为 @RequestMapping(value =“ / admin / login”,方法= RequestMethod.GET)